// Copyright (c) 2022, hzmes and contributors
// For license information, please see license.txt

frappe.ui.form.on('SRM Receive', {
	refresh: function(frm) { 
		frm.add_custom_button(__("SRM Delivery Checking"), function() {
			const current_doc = $('.data-row.editable-row').parent().attr("data-name");
			const item_row = locals["SRM Receive Sub"][current_doc];
			if (item_row.reveive_qty > item_row.check_qty) {
				frappe.db.get_list('SRM Purchase Delivery Sub', {
					fields: ['name', 'parent', 'creation', 'remain_qty'],
					filters: {
						po_entryid: item_row.erp_po_entryid,
						remain_qty: ['>', 0],
					}
				}).then((r) => {
					if (r.length > 0){
						const d = new frappe.ui.Dialog({
							title: __('核销送货数量'),
							width: 400
						});
						$(`<div class="modal-body ui-front">
							<h2>物料:${item_row.material_number} 收料数量:${item_row.reveive_qty} 已核销:${item_row.check_qty}</h2>
							<table class="table table-bordered">
							<thead>
								<tr>
								<th>选择</th>
								<th>单据号</th>
								<th>日期</th>
								<th>数量</th>
								</tr>
							</thead>
							<tbody>
							</tbody>
							</table>
						</div>`).appendTo(d.body);
						r.forEach(element => {
							const tbody = $(d.body).find('tbody');
							const tr = $(`
							<tr>
								<td><input type="checkbox" class="check-delivery" data-row-name="${element.name}"></td>
								<td>${element.parent}</td>
								<td>${element.creation}</td>
								<td>${element.remain_qty }</td>
							</tr>
							`).appendTo(tbody)
							tbody.find('.check-delivery').on('change', function() {
								$('input.data-row-name').not(this).prop('checked', false);  
							});
						});
						d.set_primary_action(__("核销"), function() {
							$(d.body).find('input:checked').each(function(i, input) {
								console.log($(input).attr('data-row-name'));
								console.log(frm.doc.srm_supplier);
								console.log(item_row.material_number, item_row.uom, item_row.reveive_qty)
								frappe.call({
									method: 'srm.srm.doctype.srm_delivery_checking.srm_delivery_checking.relation_create',
									args: {
										supplier: frm.doc.srm_supplier, 
										po_entryid: item_row.erp_po_entryid, 
										srm_receive_sub: item_row.name,
										delivery_sub_name: $(input).attr('data-row-name'), 
										material_number: item_row.material_number,
										reveive_qty: item_row.reveive_qty,
										po_billno: item_row.po,
										uom: item_row.uom,
									},
										// disable the button until the request is completed
									btn: $('.primary-action'),
									// freeze the screen until the request is completed
									freeze: true,
									callback: (r) => {
										// on success
										console.log(r);
										frm.refresh();
									},
									error: (r) => {
										// on error
										console.log(r)
									}
								})
							// frappe.model.set_value(item_row.doctype, item_row.name, 'warehouse', $(input).attr('data-row-name'));
							});
							cur_frm.rec_dialog.hide();
							cur_frm.refresh();
						});
						cur_frm.rec_dialog = d;
						d.show();  
					}
					else(frappe.msgprint(__('No Matching Record Lines')))
				})
			}
			else (frappe.msgprint('已经核销完成'))
		})
		
	},
});

// frappe.ui.keys.add_shortcut({
// 	shortcut: 'ctrl+q',
// 	action: function(frm) { 
// 			const current_doc = $('.data-row.editable-row').parent().attr("data-name");
// 			const item_row = locals["SRM Receive Sub"][current_doc];
// 			frappe.db.get_list('SRM Purchase Delivery Sub', {
// 				fields: ['name', 'parent', 'creation', 'real_qty'],
// 				filters: {
// 					po_entryid: item_row.erp_po_entryid,
// 					cross_check_status: ['!=', 1],
// 				}
// 			}).then((r, frm) => {
// 				if (r.length > 0){
// 					const d = new frappe.ui.Dialog({
// 						title: __('核销送货数量'),
// 						width: 400
// 					});
// 					$(`<div class="modal-body ui-front">
// 						<h2>${item_row.material_number} ${item_row.material_name} ${item_row.material_model}</h2>
// 						<table class="table table-bordered">
// 						<thead>
// 							<tr>
// 							<th>选择</th>
// 							<th>单据号</th>
// 							<th>日期</th>
// 							<th>数量</th>
// 							</tr>
// 						</thead>
// 						<tbody>
// 						</tbody>
// 						</table>
// 					</div>`).appendTo(d.body);
// 					r.forEach(element => {
// 						const tbody = $(d.body).find('tbody');
// 						const tr = $(`
// 						<tr>
// 							<td><input type="checkbox" class="check-delivery" data-row-name="${element.name}"></td>
// 							<td>${element.parent}</td>
// 							<td>${element.creation}</td>
// 							<td>${element.real_qty }</td>
// 						</tr>
// 						`).appendTo(tbody)
// 						tbody.find('.check-delivery').on('change', function() {
// 							$('input.data-row-name').not(this).prop('checked', false);  
// 						});
// 					});
// 					d.set_primary_action(__("核销"), function() {
// 						$(d.body).find('input:checked').each(function(i, input) {
// 							console.log($(input).attr('data-row-name'));
// 							console.log(frm.doc.srm_supplier);
// 							console.log(item_row.material_number)
// 							// frappe.call({
// 							// 	method: 'srm.srm.doctype.srm_delivery_checking.srm_delivery_checking.relation_create',
// 							// 	args: {
// 							// 		supplier: frm.doc.srm_supplier, 
// 							// 		po_billno: item_row.po, 
// 							// 		po_entryid: item_row.erp_po_entryid, 
// 							// 		material_number: item_row.material_number, 
// 							// 		delivery_sub_name: $(input).attr('data-row-name'), 
// 							// 		uom: item_row.uom, 
// 							// 		delivery_qty: item_row, 
// 							// 		check_qty: item_row.reveive_qty
// 							// 	},
// 							// 	    // disable the button until the request is completed
// 							// 	btn: $('.primary-action'),
// 							// 	// freeze the screen until the request is completed
// 							// 	freeze: true,
// 							// 	callback: (r) => {
// 							// 		// on success
// 							// 	},
// 							// 	error: (r) => {
// 							// 		// on error
// 							// 	}
// 							// })
// 						// frappe.model.set_value(item_row.doctype, item_row.name, 'warehouse', $(input).attr('data-row-name'));
// 						});
// 						cur_frm.rec_dialog.hide();
// 						cur_frm.refresh_fields();
// 					});
// 					cur_frm.rec_dialog = d;
// 					d.show();  
// 				}
// 				else(frappe.msgprint(__('No Matching Record Lines')))
// 			})
   
// 	},
// 	page: this.page,
// 	description: __('核销送货数量'),
// 	ignore_inputs: true,
	
// });



// frappe.call({
// 	method: 'erpnext.stock.dashboard.item_dashboard.get_data',
// 	args: {
// 		item_code: item_row.item_code,
// 	},
// 	callback: function(r) {
// 		if (r.message.length > 0){
// 			const d = new frappe.ui.Dialog({
// 				title: __('Item Balance'),
// 				width: 400
// 			});
// 			$(`<div class="modal-body ui-front">
// 				<h2>${item_row.item_code}</h2>
// 				<p>Choose Warehouse and click Select :</p>
// 				<table class="table table-bordered">
// 				<thead>
// 					<tr>
// 					<th>Check</th>
// 					<th>Warehouse</th>
// 					<th>Qty</th>
// 					<th>UOM</th>
// 					</tr>
// 				</thead>
// 				<tbody>
// 				</tbody>
// 				</table>
// 			</div>`).appendTo(d.body);
// 			r.message.forEach(element => {
// 				const tbody = $(d.body).find('tbody');
// 				const tr = $(`
// 				<tr>
// 					<td><input type="checkbox" class="check-warehouse" data-warehouse="${element.warehouse}"></td>
// 					<td>${element.warehouse}</td>
// 					<td>${element.actual_qty}</td>
// 					<td>${item_row.stock_uom }</td>
// 				</tr>
// 				`).appendTo(tbody)
// 				tbody.find('.check-warehouse').on('change', function() {
// 					$('input.check-warehouse').not(this).prop('checked', false);  
// 				});
// 			});
// 			d.set_primary_action("Select", function() {
// 				$(d.body).find('input:checked').each(function(i, input) {
// 				frappe.model.set_value(item_row.doctype, item_row.name, 'warehouse', $(input).attr('data-warehouse'));
// 				});
// 				cur_frm.rec_dialog.hide();
// 				cur_frm.refresh_fields();
// 			});
// 			cur_frm.rec_dialog = d;
// 			d.show();  
// 		}
// 	}
// });  